import {
  queryTableDataInfoListReq,
  addDictionaryInfoReq,
  updateDictInfoReq,
  queryInfoReq,
  delDictInfoReq,
  queryDictNameOptionReq,
} from "./service";
import { message } from "antd";
import { formatDict } from "@/utils/format";
export default {
  namespace: "SystemDictipnaryDetail",
  state: {
    init: false,
    total: "",
    dictInfoList: [],
    dictIdList: [],
    dictNameList: [],
    formParams: {},
  },
  // 异步获取
  effects: {
    *pageInit({ params }: any, { put }: any) {
      yield put({ type: "queryTableDataInfoList", params });
      yield put({ type: "queryDictNameOption" });
    },
    *queryTableDataInfoList({ params }: any, { put, select }: any) {
      const { formParams } = yield select(
        (state: any) => state.SystemDictipnaryDetail
      );
      let _params = {};
      Object.assign(
        _params,
        params && Object.keys(params).length !== 0 && formParams,
        !params && { pageSize: formParams.pageSize },
        params,
        !params?.pageNum && { pageNum: 1 }
      );
      const { code, msg, rows, total } = yield queryTableDataInfoListReq(
        _params
      );
      if (code === 200) {
        yield put({
          type: "save",
          payload: {
            dictInfoList: rows,
            total,
            formParams: _params,
            pageNumber: params?.pageNum,
            pageSize: params?.pageSize,
          },
        });
      } else {
        message.error(msg);
      }
    },

    // 查询
    *queryInfo({ params, callback }: any, { call, put }: any) {
      const { code, data } = yield call(queryInfoReq, params);
      if (code === 200) {
        yield put({
          type: "save",
          payload: {
            dictIdList: data,
          },
        });
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    // 增加
    *addDictionaryInfo({ params, callback }: any, { call }: any) {
      const { code } = yield call(addDictionaryInfoReq, params);
      if (code === 200) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    // 修改
    *updateDictInfo({ params, callback }: any, { call }: any) {
      const { code } = yield call(updateDictInfoReq, params);
      if (code === 200) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    // 删除
    *delDictInfo({ params, callback }: any, { call }: any) {
      const { code } = yield call(delDictInfoReq, params);
      if (code === 200) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        callback && callback();
      }
    },
    // 获取字典名称下拉框分页
    *queryDictNameOption({ params }: any, { call, put }: any) {
      const { code, data } = yield call(queryDictNameOptionReq, params);
      if (code === 200) {
        yield put({
          type: "save",
          payload: {
            dictNameList: formatDict(data, "dictName", "dictType"),
          },
        });
      }
    },
  },
  // 同步存储
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
